package com.fanshuai.soring.boot.mvc.metrics;

import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

public class MetricsHandlerAdapter implements HandlerAdapter {
    @Override
    public boolean supports(Object o) {
        return o instanceof MetricsOutput;
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        MetricsOutput output = (MetricsOutput) o;

        String metrics = output.metrics();
        response.setContentType("text/plain; charset=UTF-8");
        response.getOutputStream().write(metrics.getBytes(StandardCharsets.UTF_8));
        response.getOutputStream().flush();

        return null;
    }

    @Override
    public long getLastModified(HttpServletRequest request, Object o) {
        return 0;
    }
}
